#using <System.dll>
#include <direct.h>
#include <stdio.h>
#include <windows.h>
#include <fstream>
#include <vector>
#include "Unzipper.cpp"

using namespace std;
using namespace System;

string parseRTF(const char* RTFFilePath)
{
	ifstream RTFFile(RTFFilePath);
	string s = "";
	string par_str = "";
	int curly_bracket_depth = -1;       //ignore the first curly bracket
	bool redundant_backslash = false;
	bool primary_backslash = false;
	bool ignore_space = false;
	char c;
	
	while (RTFFile)
	{
		RTFFile.get(c);

		if (c == '{')
		{
			if (primary_backslash)
			{
				s = s + '{';
				primary_backslash = false;
				continue;
			}

			else
			{
				curly_bracket_depth++;
				continue;
			}
		}

		
		if (curly_bracket_depth > 0)
		{
			if (c == '}')
				curly_bracket_depth--;
			continue;
		}

		if (c == '}')
		{
			if (primary_backslash)
			{
				s = s + '}';
				primary_backslash = false;
				continue;
			}
		
			else
			{
				curly_bracket_depth--;
				continue;
			}
		}

		if (ignore_space)
		{
			ignore_space = false;

			if (c == ' ')
				continue;
		}


		if (redundant_backslash)
		{
			if (c == ' ')
				redundant_backslash = false;
			continue;
		}

		if (primary_backslash)
		{
			if ((c == 'a') || (c == 'b') || (c == 'p') || (c == 'r') || (c == 't'))
			{
				par_str = par_str + c;

				if (par_str.size() == 3)
				{
					if (par_str == "tab")
					{
						s = s + "\t";
						par_str = "";
						primary_backslash = false;
						ignore_space = true;
						continue;
					}
				
					else if (par_str == "par")
					{
						s = s + "\n";
						par_str = "";
						primary_backslash = false;
						ignore_space = true;
						continue;
					}
	
					else
					{
						par_str = "";
						primary_backslash = false;
						ignore_space = true;	
						continue;
					}
				}

				continue;	
			}
		
			else if (c == ' ')
			{
				par_str = "";
				primary_backslash = false;	
				continue;
			}

			else if (c == '\\')
			{
				par_str = "";
				primary_backslash = false;
				s = s+c;
				continue;
			}

			else
			{
				par_str = "";
				redundant_backslash = true;
				primary_backslash = false;	
				continue;
			}
		}


		if (c == '\\')
		{
			primary_backslash = true;
			continue;
		}
		
		s = s + c;
	}

	s[s.size()-1] = 0;  //remove final newline

	return s;
}

string parseXML(const char* XMLFilePath)
{
	bool b = false;  //bracket check
	bool ampersand = false; //ampersand check
	bool gamma = false; //gamma check (for quotes)
	string b_temp;
	string amp_temp;
	string gamma_temp;
	char c;
	string s = "";
	ifstream aStream(XMLFilePath);

	while (aStream)
	{
		aStream.get(c);
		
		if (c == '<')
		{
			b = true;
			continue;
		}

		if (c == '>')
		{
			b = false;
			if (b_temp == "text:tab/" || b_temp == "w:tab/")
				s += "\t";
			else if (b_temp == "text:s/")
				s += " ";
			else if (b_temp == "/text:p" || b_temp == "/w:p")
				s += "\n";
			b_temp = "";
			continue;
		}

		if (c == '&')
		{
			ampersand = true;
			continue;
		}

		if (ampersand)
		{
			if (c == ';')
			{
				if (amp_temp == "amp")
					s += "&";
				else if (amp_temp == "gt")
					s += ">";
				else if (amp_temp == "lt")
					s += "<";
				amp_temp = "";
				ampersand = false;
			}

			else
				amp_temp += c;

			continue;
		}

		if (c == -30)
		{
			gamma = true;
			continue;
		}

		if (gamma)
		{

			gamma_temp += c;

			if (gamma_temp.size() == 2)
			{
				if (gamma_temp[1] > -103)
					s += "\"";
				else
					s += "\'";
				gamma_temp = "";
				gamma = false;
			}
			
			continue;

		}
		
		if (!(b))
			s += c;
		else
			b_temp = b_temp+c;
	}

	s[s.size()-1] = 0; //remove final newline
	
	return s;
}

string parseDocx(const char* docxFilePath)
{
	string s;

	char documentName[128];
	_splitpath(docxFilePath, NULL, NULL, documentName, NULL);

	string unzipFolderPath;
	unzipFolderPath.append(documentName).append("-docx");
	
	CUnzipper unzipFile(docxFilePath);
	unzipFile.UnzipTo(unzipFolderPath.c_str(), true);

	s = parseXML((unzipFolderPath+"\\word\\document.xml").c_str());

	//delete unzipfolderpath folder

	return s;
}


string parseODT(const char* ODTFilePath)
{
	string s;

	char documentName[128];
	_splitpath(ODTFilePath, NULL, NULL, documentName, NULL);

	string unzipFolderPath;
	unzipFolderPath.append(documentName).append("-odt");
	
	CUnzipper unzipFile(docxFilePath);
	unzipFile.UnzipTo(unzipFolderPath.c_str(), true);

	s = parseXML((unzipFolderPath+"\\content.xml").c_str());

	//delete unzipfolderpath folder

	return s;
}